<?php

/**
 * This file is part of the Codizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Codizer Core Team <team@codizer.org>
 * @package     Codizer
 */

/**
 * Retrieve and display a block from a block_action
 *
 * @param module : the module name
 * @param action : the action name
 * @param ...... : additionnal block parameters
 * 
 * @return string|html|xml|...
 * 
 * @example {block module="backoffice" action="loremIpsum" paragraphs="5"}
 */
function smarty_function_block($params, &$smarty)
{
	// === no module and no action defined, do nothing
	if(!is_string($params['module']) && !is_string($params['action']))
	{
		return "";
	}
		
	// === initialize block context
	$blockContext = f_context_Block::getInstance();
	
	// === remove old blocks parameters
	$blockContext->removeParameters();
	
	// === add current additionnal block parameters to block context
	$notToTake = array("module", "action");
	foreach($params as $key => $value)
	{
		if(!in_array($key, $notToTake))
		{
			$blockContext->setParameter($key, $value);
		}
	}
	
	// === initialize the main controller
	$controller = f_core_Controller::getInstance();
	
	// === disable exception messages
	$controller->disableException();
	
	// === dispatch the action
	return $controller->setModule($params['module'])->setAction("block_".$params['action'])->dispatch();
}